//
//  CookBookCollectionViewCell.m
//  Taste
//
//  Created by 徐昊 on 16/9/3.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "CookBookCollectionViewCell.h"
#import "HXMacro.h"
#import "CookBookLitleCollectionViewCell.h"
#import "CookBookModel.h"
#import "ZWYNetTool.h"
#import "MasterCookBookViewController.h"
#import "CookBookViewController.h"
#import "CookBookRes.h"

#define kCookBookLitleCollectionViewCellName @"CookBookLitleCollectionViewCellName"
@interface CookBookCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) CookBookModel *model;


@end

@implementation CookBookCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 15) / 2, ((SCREEN_WIDTH / 2) * 675 / 1200) + 30);
    _layout.minimumInteritemSpacing = 5;
    _layout.minimumLineSpacing = 5;
    _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"CookBookLitleCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCookBookLitleCollectionViewCellName];
    
}
- (void)setNum:(NSInteger)num {
    _num = num;
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : @"22"};
    NSString *string = [NSString stringWithFormat:@"ord=1&page=1&type2id=%ld", _num];
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=cookbook&a=getlist" andBody:string andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [CookBookModel modelWithDic:result];
        
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
        
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _model.res.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CookBookLitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCookBookLitleCollectionViewCellName forIndexPath:indexPath];
    cell.res = _model.res[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MasterCookBookViewController *vc = [[MasterCookBookViewController alloc] init];
    CookBookViewController *homeVC = (CookBookViewController *)self.nextResponder.nextResponder.nextResponder;
    vc.numString = _model.res[indexPath.row].cookBookRes_id;
    [homeVC.navigationController pushViewController:vc animated:YES];
}
@end
